package org.csu.mypetstore.controller;

import jakarta.annotation.Resource;
import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.mapper.CartMapper;
import org.csu.mypetstore.mapper.OrderStatusMapper;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.OrderStatusService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api")
@MapperScan("org.csu.mypetstore.mapper")
public class ApiController {

    @Resource
    private CartMapper cartMapper;

    @Resource
    private OrderStatusMapper orderStatusMapper;

    @Resource
    private CatalogService catalogService;
    @Resource
    private OrderStatusService orderStatusService;
    @Resource
    private AccountService accountService;
    @Resource
    private CartService cartService;



    @GetMapping("/items")
    public CommonResponse<List<Item>> getItems(){
        int code = catalogService.getItemList().getStatus();
        if(code == 1){
            return CommonResponse.createForError(catalogService.getItemList().getMessage());
        }
        List<Item> itemList = catalogService.getItemList().getData();
        return CommonResponse.createForSuccess("查询items成功", itemList);
    }





    @Transactional
    @PostMapping("/orders/buy")
    public CommonResponse<Object> buy(@RequestBody Account account) {
        List<Cart> cartList =accountService.getMyCartByUserid(account.getAccountId()).getData();
        BigDecimal subTotal = cartService.getSubCost(cartList);
        String accountId = account.getAccountId();
        System.out.println(account);


        int store = cartList.size();
        if (store == 0) {
            return CommonResponse.createForError("购物车为空，无法进行支付");
        }


        Date date = new Date();
        OrderStatus orderStatus = new OrderStatus();
        orderStatus.setCreatetime(date);
        orderStatus.setUserid(account.getUserId());
        orderStatus.setOrderid(orderStatusService.getTraceNo());
        orderStatus.setTotal(subTotal);
        orderStatus.setAddr1(account.getAddress1());
        orderStatus.setAddr2(account.getAddress2());
        orderStatus.setPhone(account.getPhone());


        boolean flag;
        // 当商品数量不大于库存时返回true,否则返回false
        flag = catalogService.checkItemQuantity(cartList);
        if(!flag){
            return CommonResponse.createForError("购物车商品数大于存储量，无法进行支付");
        }
        // 当成功将order存入数据库时返回true，否则返回false
        flag = orderStatusService.createOrder(orderStatus);

        if(!flag){
            return CommonResponse.createForError("生成订单失败!");
        }


        return CommonResponse.createForSuccess( "生成订单成功!", orderStatus);
    }
}